<?php

namespace App\Http\Controllers\Auth;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\UserInsertRequest;
use App\Http\Requests\UpdateUserRequest;
use App\User;
use Hash;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $keywords = $request->input('keywords');
        // dd($keywords);die;
        if(empty($keywords)){
            $users =User::orderBy('id','desc')->paginate($request->input('num',10));
        }else{
            $users = User::orderBy('id','desc')->where('username','like','%'.$keywords.'%')->paginate($request->input('num',10));
        
        }
        // dd($users);
        return view('admin.user.index',['users'=>$users,'data'=>$request->all()]); 
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
        return view('admin.user.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(UserInsertRequest $request)
    {
        //
         // 数据入库
        $user = new User;
        $user -> username = $request->username;
        $user -> password = Hash::make($request->input('password'));
        $user -> email = $request->email;
        $user -> age = 20;
        // dd($user);die;
        if($request -> hasFile('img')){
            //创建文件的名字
            $filename = time().rand(10000,99999);
            //获取文件的后缀
            $suffix = $request->file('img')->getClientOriginalExtension();
            //上传路径
            $dirname = './uploads/';
            //文件名
            $name = $filename.'.'.$suffix;
            //上传头像
            $request->file('img')->move($dirname,$name);
            //
            $user -> img = trim($dirname.$name,'.');
        }

        if($user->save()){
            // echo 222;
            return redirect('/user');
        }else{
            // echo 3333;
            return back();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
